
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>     // for exit()
#include <fcntl.h>      // for open()
#include <sys/wait.h>
#include <signal.h>


int myFork (void);
pid_t my_fork (void);
int my_open ( char *file, int flags);
int my_close (int fd);
int my_read ( int fd, void *buffer, int num_bytes );
int my_write ( int fd, void *buffer, int num_bytes );
pid_t my_waitpid (pid_t pid, int *status_ptr, int options);
int my_kill (pid_t pid, int sig);
int my_sigpending (sigset_t *set);
int my_pipe (int pipefd[2]);

pid_t my_fork(void) {

    pid_t res = fork();

    // abort program if assertion is false
    // continue if statement is true
    assert( res != -1 );

    return res;
}


int myFork() {
    
  int res = 0;

  res = fork();
  
  if (res < 0) {
      printf("Error in fork() \n");
      // perror("fork()");
      exit(1);        
  }
      
  else
     return res; 
}


int my_open( char *file, int flags) {

    int res = open(file, flags);

    assert(res != -1);
    return res;
}


int my_read( int fd, void *buffer, int num_bytes ) {

    int bytes_read = read(fd, buffer, num_bytes);

    if ( bytes_read == -1 ) {

        perror("read()");
        exit(1);
    }

    assert( bytes_read != -1 );
    return bytes_read;
}


int my_write( int fd, void *buffer, int num_bytes ) {

    int bytes_write = write(fd, buffer, num_bytes);

    assert( bytes_write != -1 );
    return bytes_write;
}


int my_close(int fd) {

    int res = close(fd);
    assert ( res != -1 );
    return res;
}


pid_t my_waitpid(pid_t pid, int *status_ptr, int options) {

    pid_t res = waitpid(pid, status_ptr, options);
    assert ( res != -1 );
    return res;
}


int my_kill(pid_t pid, int sig)
{
    int res = kill(pid, sig);

    if (res < 0)
        perror("kill()");

    assert( res != -1);
    return res;
}


int my_sigpending (sigset_t *set) 
{
    int res = sigpending (set);

    assert (res != -1);
    return res;
}


int my_pipe (int pipefd[2])
{
    int res = pipe (pipefd);

    assert (res != -1);
    return res;
}
